home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 615 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.3 KB

  1. From: kanze@gabi-soft.fr (J. Kanze)
  2. Message-ID: <KANZE.96Mar4112305@gabi.gabi-soft.fr>
  3. X-Original-Date: 04 Mar 1996 10:23:05 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 04 Mar 96 12:47:37 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Streams and eof()
  9. Followup-To: comp.std.c++
  10. Organization: GABI Software, Sarl.
  11. References: <199603010214.VAA16273@pleiades.cs.rpi.edu>
  12. In-Reply-To: axl@zedat.fu-berlin.de's message of 01 Mar 1996 17:43:16 PST
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMTrmd+EDnX0m9pzZAQG+OwGAm2CbNgZwqMywqjF3WeUIiFASGQRNpUTh
  15.     aSe1DpnAZIIRF6zePilWWkP40uyBi76Y
  16.     =CFX2
  17.  
  18. In article <199603010214.VAA16273@pleiades.cs.rpi.edu>
  19. axl@zedat.fu-berlin.de (Axel Thimm) writes:
  20.  
  21. |> I have had a look in the April draft, but I think that there is a small
  22. |> ambiguity about eof() methods in streams.
  23. |> Is the eof-flag raised, when the end of the stream is reached, or when
  24. |> an attempt to move past this end is made?
  25.  
  26. Yes.
  27.  
  28. I've not reverified the exact words in the latest draft, but in previous
  29. versions, it was undefined exactly when the EOF flag would be set.  For
  30. this reason, the eof() function is, in fact, practically useless.
  31. (IMHO, of course.)  On the other hand, even if it were well defined, it
  32. would probably not be as useful as one would like, for reasons explained
  33. below. 
  34.  
  35. |> I think the most common
  36. |> practice is the latter, but the ANSI draft uses the former statement.
  37.  
  38. The common practice is that the implementation will sometimes do one,
  39. sometimes the other.  For example:
  40.  
  41.     char                str[]( "12" ) ;
  42.     istrstream          s( str ) ;
  43.     int                 i ;
  44.     s >> i ;
  45.     //  s.eof() is generally true in most implementations
  46.  
  47. But:
  48.  
  49.     char                str[]( "12x" ) ;
  50.     istrstream          s( str ) ;
  51.     int                 i ;
  52.     char                c ;
  53.     s >> i >> c ;
  54.     //  s.eof() is generally false in most implementations
  55.  
  56. Note that although the results of s.eof() are different, the position of
  57. the read mark relative to the end of file is exactly the same in both
  58. cases: the last read succeeded, but the next one will fail.
  59.  
  60. |> Declaring the end of a stream as the first charachter _after_ the last
  61. |> character of the stream would rescue the expression, but wouldn't fit
  62. |> into a concept of an end. (Usually one thinks of an end as belonging to
  63. |> the whole)
  64. |> As I am rather convinced, that the eof is raised at an attempt to meove
  65. |> past the end, I'd like to ask how to check this in a binary file without
  66. |> having to perform a movement.
  67.  
  68. You might want to try the function `peek'.  I tend to use this function
  69. a lot.  Basically, this forces a look-ahead, which will set the eof if
  70. there is nothing more there.
  71.  
  72. Historically, C has generally used the eof condition to indicate that
  73. the last read failed (because of eof); other languages (at least,
  74. Pascal), have used it to indicate that the next read will fail.  From a
  75. programming point of view, the latter is more useful, but impossible to
  76. implement correctly for formatting input.  Consider the following:
  77.  
  78.     char                str[]( "    " ) ;
  79.     istrstream          s( str ) ;
  80.  
  81. If the next operation on s is operator>>( istream& , int& ), the
  82. operation will fail (because of end of file), and so a predictive eof()
  83. should return true.  If the next operation is get(), however, it will
  84. succeed, so a predictive eof() should return false.
  85.  
  86. The solution is to test for failure (fail()) after conversion, when
  87. using formatted input.  For character-wise input (either binary or not),
  88. you have the choice: test for failure after, or use peek to test for eof
  89. before.  Once failure has been detected, you may use eof() to determine
  90. if this was the reason for the failure.
  91. -- 
  92. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  93. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  94. Conseils, itudes et rialisations en logiciel orienti objet --
  95.               -- A la recherche d'une activiti dans une region francophone
  96. ---
  97. [ To submit articles: try just posting with your news-reader.
  98.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  99.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  100.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  101.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  102. ]
  103.